package com.service.sys.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.config.globalException.BusinessException;
import com.entity.PersonnelTransfer;
import com.mapper.sys.UserVoMapper;
import com.service.PersonnelTransferService;
import com.service.sys.UserVoService;
import com.vo.sys.NewPassParam;
import com.vo.sys.UserExceptSelfVo;
import com.vo.sys.UserVo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserVoServiceImpl extends ServiceImpl<UserVoMapper, UserVo> implements UserVoService {
    @Override
    public UserVo getByUserName(String username) {
        List<UserVo> userVoList = this.list(new QueryWrapper<UserVo>().eq(
                "username", username
        ));
        //假设不要这段代码可以吗
        if (userVoList.size() == 0) {
            return null;
        }
        return userVoList.get(0);
    }

    /**
     * 通过主键更新。如果修改了部门，则需要向部门修改表去申请一条信息
     *
     * @param userVo
     * @return
     */

    @Resource
    private PersonnelTransferService personnelTransferService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateByPrimary(UserVo userVo) {
        UserVo byId = this.getById(userVo.getId());
        if (!byId.getDepartmentId().equals(userVo.getDepartmentId())) {
            //判断是否有已申请但是未审核的信息，如果有，则给提示
            QueryWrapper<PersonnelTransfer> wrapper = new QueryWrapper<>();
            wrapper.eq(
                    "user_id", userVo.getId()
            ).eq(
                    "statu", "0"
            );
            List<PersonnelTransfer> transferList = personnelTransferService.list(wrapper);
            if (CollectionUtil.isNotEmpty(transferList)) {
                throw new BusinessException("请勿频繁申请人员调动");
            }
            //修改
            PersonnelTransfer personnelTransfer = new PersonnelTransfer();
            personnelTransfer.setUserId(userVo.getId());
            personnelTransfer.setBeforeDepartment(byId.getDepartmentId());
            personnelTransfer.setAfterDepartment(userVo.getDepartmentId());
            personnelTransfer.setStatu("0");
            personnelTransferService.save(personnelTransfer);
        }
        userVo.setDepartmentId(byId.getDepartmentId()); //部门仍然为之前的部门
        return this.updateById(userVo);
    }

    @Resource
    private UserVoMapper userVoMapper;

    @Override
    public List<UserExceptSelfVo> getUserVos(String username) {
        return userVoMapper.selectUserVos(username);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean changePassword(NewPassParam userVoAndNewPassParam) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        //对密码进行加密
        String encode = encoder.encode(userVoAndNewPassParam.getCheckPass());
        System.out.println("encode = " + encode);
        boolean matches = encoder.matches(userVoAndNewPassParam.getCheckPass(), encode);
        System.out.println("matches = " + matches);
        //使用Mybatis-Plus进行更新
        UpdateWrapper<UserVo> newPassParamUpdateWrapper = new UpdateWrapper<>();
        newPassParamUpdateWrapper.eq("id", userVoAndNewPassParam.getId())
                .eq("username", userVoAndNewPassParam.getUsername())
                .set("password", encode);
        int update = userVoMapper.update(null, newPassParamUpdateWrapper);
        if (update > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<UserExceptSelfVo> getUserVosByLike(String username,String inputName) {
        return userVoMapper.selectUserVosByLike(username,inputName);
    }
}
